<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\CategoryRequest;
use App\models\Category;
use Illuminate\Http\Request;

class CategoryController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
//        $categories = $category->getAllCategory();
//        dd($categories);
        $categories = Category::paginate(6);
        return view('admin.category_index',compact('categories'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Category $category)
    {
        $categories = Category::get();
        return view('admin.category_create',compact('categories'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(CategoryRequest $request)
    {
        $data = $request->all();
        if (!isset($data['icon'])){
            $data['icon'] = 'fe fe-layout';
        }
        Category::create($data);
        return redirect(route('admin.category.index'))->with('success','添加分类成功');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\models\Category  $category
     * @return \Illuminate\Http\Response
     */
    public function show(Category $category)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\models\Category  $category
     * @return \Illuminate\Http\Response
     */
    public function edit(Category $category)
    {
//       dd(123);
        $categories = $category->getAllCategory();
//        dd($categories);
        return view('admin.category_edit',compact('category','categories'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\models\Category  $category
     * @return \Illuminate\Http\Response
     */
    public function update(CategoryRequest $request, Category $category)
    {
        $data = $request->all();
        if (!isset($data['icon'])){
            $data['icon'] = 'fe fe-layout';
        }
        $category->update($data);
        return redirect(route('admin.category.index'))->with('success','编辑成功');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\models\Category  $category
     * @return \Illuminate\Http\Response
     */
    public function destroy(Category $category)
    {
        if ($category->hasChild()){
            return back()->with('error','请优先删除当前分类的子分类');
        }
       $category->delete();
       return redirect(route('admin.category.index'))->with('success','删除成功');
    }

//    获取子类的方法
    public function getSonCategory($id)
    {
        $categorys = Category::where('parent_id',$id)->get();
        return $categorys;
    }
}
